package com.hqj.client.controller;

import com.hqj.client.entity.ClientUser;
import com.hqj.client.service.ClientUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 王建林
 * @version 1.0
 * @date 2020/10/14
 */
@Api(tags = "客户端登陆注册")
@RequestMapping("/users")
@Controller
public class ClientUserController {
    @Resource
    private ClientUserService ClientuserService;

    @ApiOperation("登陆判断")
    @PostMapping("/login")
    public String login(@RequestParam String username, @RequestParam String password, Model model) {
        ClientUser user = ClientuserService.login(username, password);
        Map<String, String> map = new HashMap<>();
        if (user == null) {
            map.put("msg", "用户不存在");
            model.addAttribute(map);
            return "/page/BlogLogin";
        } else if (!password.equals(user.getPassword())) {
            map.put("msg", "密码不存在");
            model.addAttribute(map);
            return "/page/BlogLogin";
        } else if (password.equals(user.getPassword())) {
            map.put("msg", "登陆成功");
            model.addAttribute(map);
            return "/page/BlogIndex";
        }
        return "/page/BlogLogin";
    }

    @ApiOperation("邮箱发送验证码")
    @PostMapping("/emailSet")
    public String emailSet(String email, HttpServletRequest request, HttpServletResponse response,Model model) {
        //当前毫秒值
        long millis = System.currentTimeMillis();
        System.out.println("--------------millis:"+millis);
        String emailSet = "";
        HttpSession session = request.getSession();
        try {
            long attribute = (long) session.getAttribute("email");
            if(System.currentTimeMillis()-attribute<60000){
              model.addAttribute("msg","不要重复发送");
            }

        } catch (Exception e) {
             emailSet = ClientuserService.emailSet(email);
             session.setAttribute(email,System.currentTimeMillis());
        }
        //存储验证码
        session.setAttribute("code", emailSet);
        //存储发送验证码的毫秒值
        session.setAttribute("millis",millis);
        return "success";
    }

    @ApiOperation("发送验证码注册")
    @PostMapping("/register")
    public String register(String username,
                           String password,
                           String email,
                           String code, HttpServletRequest request, Model model) {
        HttpSession session = request.getSession();
        //取出发送验证码的毫秒值
        long millis = (long)session.getAttribute("millis");
        System.out.println("--------------millis:"+millis);
        //当前毫秒值
        long l = System.currentTimeMillis();
        System.out.println("--------------l:"+l);
        if((l-millis)>=60000){
            model.addAttribute("millis","验证码过期,请重新发送验证码");
            return "/page/BlogRegister";
        }
        //取出发送过来的验证码
        String codes = (String) session.getAttribute("code");
        System.out.println("发送到邮箱的验证码：" + codes);
        //验证码不相等
        if (!code.equals(codes)) {
            model.addAttribute("msg", "验证码错误");
            return "/page/BlogRegister";
        } else {
            ClientuserService.register(username, password, code, email);
            model.addAttribute("msg", "注册成功");
            return "/page/BlogLogin";
        }
    }

    @ApiOperation("修改密码")
    @PostMapping("/changePassword")
    public String changePassword(String username, String password, String newCode, Model model) {
        int i = ClientuserService.changePassword(username, password, newCode);
        if (i > 0) {
            model.addAttribute("msg", "修改成功");
        }
        return "/page/BlogLogin";
    }

    @ApiOperation("忘记密码")
    @PostMapping("/forgetPassword")
    public String forgetPassword(String username,
                                 String newCode,
                                 String authCode, HttpServletRequest request, Model model) {
        HttpSession session = request.getSession();
        //取出验证码
        String codes = (String) session.getAttribute("code");
        if (!authCode.equals(codes)){
            model.addAttribute("msg","验证码错误");
            return "/page/BlogLogin";
        }else {
            ClientuserService.forgetPassword(username,newCode,authCode);
            return "/page/BlogLogin";
        }
    }


}
